/*
 * @(#)SourceDevicePanel.java
 * 
 * Copyright(C) 2004 Yujin Kim
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package com.netexplode.jtunes.client.ui.component;

import java.util.List;

import javax.swing.JScrollPane;
import javax.swing.JTree;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreeSelectionModel;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.netexplode.jtunes.CollectionType;
import com.netexplode.jtunes.client.player.jTunes;
import com.netexplode.jtunes.model.media.CollectionManager;
import com.netexplode.jtunes.model.media.MediaCollection;

public class SourceDevicePanel extends JScrollPane {

	private static final long serialVersionUID = 1L;

	private static transient final Log log = LogFactory.getLog(SourceDevicePanel.class);

	private static JTree tree;

	private CollectionManager collectionManager;

	public void setCollectionManager(CollectionManager collectionManager) {
		this.collectionManager = collectionManager;
	}

	public void init() {
		log.info("initalizing");
		final DefaultMutableTreeNode root = new DefaultMutableTreeNode("jTunes");
		tree = new JTree(root);
		createMediaSourceNodes(root);

		tree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);

		tree.addTreeSelectionListener(new TreeSelectionListener() {
			public void valueChanged(TreeSelectionEvent e) {
				DefaultMutableTreeNode node = (DefaultMutableTreeNode) tree.getLastSelectedPathComponent();

				if (node == null) {
					return;
				}

				Object nodeInfo = node.getUserObject();
				log.debug("a node selected: " + nodeInfo.toString());
				if (node.isLeaf()) {
					log.debug("is a leaf");
					if (!node.isRoot()) {
						if (node.getParent().toString().equals("local")) {
							log.debug("parent was local");
							TreeItem item = (TreeItem) node.getUserObject();
							log.debug("item = " + item.getIndex() + " | " + item.toString());
							collectionManager.setCollectionType(CollectionType.Local);
							collectionManager.setLocalPosition(item.getIndex());

						}
						else if (node.getParent().toString().equals("shared")) {
							log.debug("parent was shared");
							TreeItem item = (TreeItem) node.getUserObject();
							log.debug("item = " + item.getIndex() + " | " + item.toString());
							collectionManager.setCollectionType(CollectionType.Shared);
							collectionManager.setSharedPosition(item.getIndex());
						}
						jTunes.refreshLibraryPanel();
					}
					else {
						log.trace("parent was not local");
					}
				}
				else {
					log.trace("is not a leaf");
				}
			}
		});

		super.setViewportView(tree);
		log.info("initalizing done");
	}

	private void createMediaSourceNodes(DefaultMutableTreeNode root) {
		final DefaultMutableTreeNode local = new DefaultMutableTreeNode("local");
		createLocalDeviceNodes(local);
		root.add(local);

		final DefaultMutableTreeNode radio = new DefaultMutableTreeNode("radio");
		root.add(radio);

		final DefaultMutableTreeNode store = new DefaultMutableTreeNode("store");
		root.add(store);

		final DefaultMutableTreeNode shared = new DefaultMutableTreeNode("shared");
		if (collectionManager.getSharedCollections() != null) {
			createSharedDeviceNodes(shared);
		}
		root.add(shared);

		final DefaultMutableTreeNode playlist = new DefaultMutableTreeNode("playlist");
		root.add(playlist);
	}

	private void createLocalDeviceNodes(DefaultMutableTreeNode local) {
		final List collections = collectionManager.getLocalCollections();
		for (int i = 0; i < collections.size(); i++) {
			MediaCollection collection = (MediaCollection) collections.get(i);
			local.add(new DefaultMutableTreeNode(new TreeItem(i, "" + collection.hashCode(), collection.getName())));
		}
	}

	private void createSharedDeviceNodes(DefaultMutableTreeNode shared) {
		final List collections = collectionManager.getSharedCollections();

		log.info("shared collection size = " + collections.size());
		for (int i = 0; i < collections.size(); i++) {
			MediaCollection collection = (MediaCollection) collections.get(i);
			shared.add(new DefaultMutableTreeNode(new TreeItem(i, "" + collection.hashCode(), collection.getName())));
		}
	}

	public void refresh() {
		this.init();
	}

	/**
	 * <code>TreeItem</code> represents .....
	 * 
	 * @author ykim
	 * @version $Revision: 1.5 $
	 * @since 0.1
	 */
	private class TreeItem {

		private int index;

		private String id;

		private String name;

		private TreeItem(int index, String id, String name) {
			this.index = index;
			this.id = id;
			this.name = name;
		}

		/**
		 * @return Returns the id.
		 */
		public String getId() {
			return this.id;
		}

		/**
		 * @param id The id to set.
		 */
		public void setId(String id) {
			this.id = id;
		}

		/**
		 * @return Returns the index.
		 */
		public int getIndex() {
			return this.index;
		}

		/**
		 * @param index The index to set.
		 */
		public void setIndex(int index) {
			this.index = index;
		}

		/**
		 * @return Returns the name.
		 */
		public String getName() {
			return this.name;
		}

		/**
		 * @param name The name to set.
		 */
		public void setName(String name) {
			this.name = name;
		}

		public String toString() {
			return this.getName();
		}
	}
}